package com.lie.prepare.merge;

import org.junit.Test;

import static com.lie.prepare.util.Print.*;
/**
 * Created by lie on 2018/4/20.
 * 这是网上找到的答案
 * 还是比较巧妙的，把最后的排序完剩下的链，直接return
 *
 * 帮助理解：现在这个递归过程，很像我在Node里赋值的过程，先从最后一个先开始赋值
 * @see Node#generateLink(int, int, int, int, int)
 */
public class ResultNodeMerge {

    public Node merge(Node n1, Node n2){
        if (n1 == null) {
            return n2;
        }
        if (n2 == null) {
            return n1;
        }
        Node result;
        if (n1.value < n2.value) {
            result = n1;
            result.nextNode = merge(n1.nextNode, n2);
        }else {
            result = n2;
            result.nextNode = merge(n1, n2.nextNode);
        }

        return result;
    }

    @Test
    public void test(){
        Node l2 = Node.getLink2();
        Node l1 = Node.getLink1();
        Node merge = merge(l1, l2);
        print(merge);

//        print("l1 = "+l1);//has changed
//        print("l2 = "+l2);
    }
}
